package mongo

import (
	"context"
	"gin/mongoc"
	"log"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type Adder struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}
type Seter struct {
	Adder
	ID string `json:"id"`
}
type Deler struct {
	ID string `json:"id"`
}

var coll = mongoc.MongoClient().Database("gin-mongo").Collection("biao1")

// FindMongo godoc
// @Summary      mongo 查询
// @Description  api描述
// @Tags         mongo
// @Accept       json
// @Produce      json
// @Param        page   query	integer  false  "页码"
// @Param        size   query	integer  false  "每页数据量"
// @Router       /mongo [get]
func FindMongo(c *gin.Context) {
	page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
	size, _ := strconv.ParseInt(c.Query("size"), 10, 64)

	Limit := size
	Skip := (page - 1) * size
	opts := &options.FindOptions{
		Limit: &Limit,
		Skip:  &Skip,
	}

	var results []bson.M
	cursor, err := coll.Find(context.TODO(), bson.D{{}}, opts)
	if err != nil {
		log.Println(err)
		c.JSON(500, gin.H{})
	}
	if err = cursor.All(context.TODO(), &results); err != nil {
		log.Println(err)
	}
	c.JSON(200, gin.H{
		"msg":  "查询成功",
		"data": results,
	})
}

// AddMongo godoc
// @Summary      mongo 添加
// @Description  api描述
// @Tags         mongo
// @Accept       json
// @Produce      json
// @Param        body   body	Adder  false "年龄和姓名"
// @Router       /mongo [post]
func AddMongo(c *gin.Context) {
	var data Adder

	if err := c.BindJSON(&data); err != nil {
		log.Println(err)
		c.JSON(500, gin.H{"err": err})
	}
	fields := bson.D{{Key: "name", Value: data.Name}, {Key: "age", Value: data.Age}}
	res, err := coll.InsertOne(context.TODO(), fields)
	if err != nil {
		log.Println(err)
		c.JSON(500, gin.H{})
	}
	c.JSON(200, gin.H{
		"msg":  "添加成功",
		"data": res,
	})
}

// SetMongo godoc
// @Summary      mongo 修改
// @Description  api描述
// @Tags         mongo
// @Accept       json
// @Produce      json
// @Param        body   body	Seter  false "id、修改后的年龄和姓名"
// @Router       /mongo [put]
func SetMongo(c *gin.Context) {
	var data Seter

	if err := c.BindJSON(&data); err != nil {
		log.Println(err)
		c.JSON(500, gin.H{"err": err})
	}
	id, _ := primitive.ObjectIDFromHex(data.ID)
	filter := bson.D{{Key: "_id", Value: id}}
	update := bson.D{{Key: "$set", Value: bson.D{{Key: "name", Value: data.Name}, {Key: "age", Value: data.Age}}}}
	_, err := coll.UpdateOne(context.TODO(), filter, update)
	if err != nil {
		log.Println(err)
	}
	c.JSON(200, gin.H{
		"msg": "修改成功",
	})
}

// DelMongo godoc
// @Summary      mongo 修改
// @Description  api描述
// @Tags         mongo
// @Accept       json
// @Produce      json
// @Param        body   body	Deler  false "要删除的id"
// @Router       /mongo [delete]
func DelMongo(c *gin.Context) {
	var data Deler

	if err := c.BindJSON(&data); err != nil {
		c.JSON(500, gin.H{"err": err})
		log.Fatal(err)
	}
	id, _ := primitive.ObjectIDFromHex(data.ID)
	filter := bson.D{{Key: "_id", Value: id}}
	_, err := coll.DeleteOne(context.TODO(), filter)
	if err != nil {
		c.JSON(500, gin.H{"err": err})
		log.Fatal(err)
	}
	c.JSON(200, gin.H{
		"msg": "删除成功",
	})
}
